<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Zookeeper Leader 选举机制深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: shimmer 3s infinite;
        }
        
        @keyframes shimmer {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .process-step {
            position: relative;
            padding-left: 40px;
            margin-bottom: 30px;
        }
        
        .process-step::before {
            content: attr(data-step);
            position: absolute;
            left: 0;
            top: 0;
            width: 30px;
            height: 30px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            font-size: 14px;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
            transition: transform 0.3s ease;
        }
        
        .feature-icon:hover {
            transform: rotate(10deg) scale(1.1);
        }
        
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .first-letter {
            float: left;
            font-size: 72px;
            line-height: 60px;
            padding-right: 8px;
            margin-top: 4px;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    Zookeeper Leader 选举机制
                </h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8">
                    深入解析分布式协调服务的核心机制
                </p>
                <div class="flex justify-center space-x-4">
                    <div class="bg-white bg-opacity-20 backdrop-filter backdrop-blur-lg rounded-lg px-6 py-3">
                        <i class="fas fa-network-wired mr-2"></i>
                        分布式系统
                    </div>
                    <div class="bg-white bg-opacity-20 backdrop-filter backdrop-blur-lg rounded-lg px-6 py-3">
                        <i class="fas fa-crown mr-2"></i>
                        Leader选举
                    </div>
                    <div class="bg-white bg-opacity-20 backdrop-filter backdrop-blur-lg rounded-lg px-6 py-3">
                        <i class="fas fa-sync mr-2"></i>
                        高可用性
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <div class="max-w-5xl mx-auto">
            
            <!-- Introduction -->
            <div class="content-card rounded-2xl p-8 mb-8">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="first-letter">Z</span>ookeeper 的 Leader 选举是一个关键的机制，用于确保在分布式环境中只有一个节点被选为 Leader，以协调和管理集群中的重要任务。这个过程的设计目标是实现高可用性、故障恢复和一致性。
                </p>
            </div>

            <!-- Key Features Grid -->
            <div class="grid md:grid-cols-3 gap-6 mb-12">
                <div class="content-card rounded-xl p-6 text-center">
                    <div class="feature-icon mx-auto">
                        <i class="fas fa-shield-alt"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-2">高可用性</h3>
                    <p class="text-gray-600">确保集群在节点故障时仍能正常运行</p>
                </div>
                <div class="content-card rounded-xl p-6 text-center">
                    <div class="feature-icon mx-auto">
                        <i class="fas fa-check-double"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-2">一致性保证</h3>
                    <p class="text-gray-600">通过ZAB协议确保数据的强一致性</p>
                </div>
                <div class="content-card rounded-xl p-6 text-center">
                    <div class="feature-icon mx-auto">
                        <i class="fas fa-bolt"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-2">快速恢复</h3>
                    <p class="text-gray-600">Leader失效时快速选举新的Leader</p>
                </div>
            </div>

            <!-- Background Section -->
            <div class="content-card rounded-2xl p-8 mb-8">
                <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                    <i class="fas fa-info-circle mr-3 text-purple-600"></i>背景与前提
                </h2>
                <p class="text-lg leading-relaxed text-gray-700 mb-4">
                    Zookeeper 集群（称为 Zookeeper Ensemble）由多个节点组成，这些节点称为服务器。集群中的节点被分为 Leader、Follower 和 Observer 三种角色。
                </p>
                <div class="grid md:grid-cols-3 gap-4 mt-6">
                    <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-4 rounded-lg">
                        <h4 class="font-bold text-purple-700 mb-2"><i class="fas fa-crown mr-2"></i>Leader</h4>
                        <p class="text-sm text-gray-600">处理写操作和协调事务</p>
                    </div>
                    <div class="bg-gradient-to-br from-blue-50 to-cyan-50 p-4 rounded-lg">
                        <h4 class="font-bold text-blue-700 mb-2"><i class="fas fa-users mr-2"></i>Follower</h4>
                        <p class="text-sm text-gray-600">处理读请求和参与选举</p>
                    </div>
                    <div class="bg-gradient-to-br from-green-50 to-teal-50 p-4 rounded-lg">
                        <h4 class="font-bold text-green-700 mb-2"><i class="fas fa-eye mr-2"></i>Observer</h4>
                        <p class="text-sm text-gray-600">处理读请求，不参与选举</p>
                    </div>
                </div>
            </div>

            <!-- Election Process Visualization -->
            <div class="content-card rounded-2xl p-8 mb-8">
                <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                    <i class="fas fa-chart-line mr-3 text-purple-600"></i>选举过程可视化
                </h2>
                <div class="mermaid">
                    graph TD
                        A[集群启动/Leader失效] --> B[所有节点进入选举状态]
                        B --> C[节点广播候选信息]
                        C --> D[包含ZXID和节点ID]
                        D --> E[节点接收并比较候选信息]
                        E --> F{获得超半数投票?}
                        F -->|是| G[成为Leader]
                        F -->|否| H[继续等待投票]
                        G --> I[广播Leader身份]
                        I --> J[其他节点成为Follower]
                        J --> K[同步数据状态]
                        H --> E
                        
                        style A fill:#f9f,stroke:#333,stroke-width:2px
                        style G fill:#9f9,stroke:#333,stroke-width:2px
                        style K fill:#99f,stroke:#333,stroke-width:2px
                </div>
            </div>

            <!-- Election Mechanism Details -->
            <div class="content-card rounded-2xl p-8 mb-8">
                <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                    <i class="fas fa-cogs mr-3 text-purple-600"></i>选举机制详解
                </h2>
                
                <div class="space-y-8">
                    <div class="process-step" data-step="1">
                        <h3 class="text-xl font-bold mb-3">节点准备</h3>
                        <p class="text-gray-700 mb-2">当 Zookeeper 集群启动时，所有的节点都会进入初始化状态。每个节点都会尝试通过选举过程来成为 Leader。节点通过交换信息、比对选举结果以及进行投票来决定谁将成为 Leader。</p>
                    </div>
                    
                    <div class="process-step" data-step="2">
                        <h3 class="text-xl font-bold mb-3">节点投票</h3>
                        <p class="text-gray-700 mb-2">每个节点在选举过程中会发出投票。投票包括节点的 ID 和当前的事务 ID（ZXID）。事务 ID 是一个递增的唯一标识符，代表集群中事务的顺序。</p>
                    </div>
                    
                    <div class="process-step" data-step="3">
                        <h3 class="text-xl font-bold mb-3">选举算法</h3>
                        <p class="text-gray-700 mb-4">Zookeeper 使用 Paxos 算法的变种，具体为基于 ZAB（Zookeeper Atomic Broadcast）协议的选举算法：</p>
                        <div class="bg-gray-50 rounded-lg p-4 space-y-3">
                            <div class="flex items-start">
                                <span class="text-purple-600 font-bold mr-3">•</span>
                                <div>
                                    <strong>候选阶段：</strong>所有节点将自己标记为候选人，并向集群中其他节点广播自己的候选信息
                                </div>
                            </div>
                            <div class="flex items-start">
                                <span class="text-purple-600 font-bold mr-3">•</span>
                                <div>
                                    <strong>投票阶段：</strong>节点接收到候选人的信息后，会进行投票并广播给其他节点
                                </div>
                            </div>
                            <div class="flex items-start">
                                <span class="text-purple-600 font-bold mr